package com.control.qct.businesssteward.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.ReplenishmentOrderAdapter;
import com.control.qct.businesssteward.base.BaseFragment;
import com.control.qct.businesssteward.model.ReplenishmentOrderModel;
import com.control.qct.businesssteward.utils.DialogUtil;
import com.control.qct.businesssteward.utils.ScreenUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/1/17 0017.
 */

public class ReplenishmentOrderFragment extends BaseFragment {
    @BindView(R.id.rv_replenishment_order)
    RecyclerView mRvReplenishmentOrder;

    private List<ReplenishmentOrderModel> mReplenishmentOrderModelList = new ArrayList<>();
    private ReplenishmentOrderAdapter mReplenishmentOrderAdapter;
    private View mSubmitView;
    private Dialog mSubmitDialog;

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_replenishment_order, container, false);
    }

    @Override
    protected void initView(View view) {
        activity.setLeftIcon(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.getSupportFragmentManager().popBackStack();
            }
        });
        activity.setTitle("补货单");
        activity.setRightIcon("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setDeleteMemberDialog();
            }
        });
        for (int i = 0; i < 6; i++) {
            mReplenishmentOrderModelList.add(new ReplenishmentOrderModel());
        }
        mRvReplenishmentOrder.setLayoutManager(new LinearLayoutManager(activity));
        mReplenishmentOrderAdapter = new ReplenishmentOrderAdapter(mReplenishmentOrderModelList);
        mRvReplenishmentOrder.setAdapter(mReplenishmentOrderAdapter);
        mReplenishmentOrderAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                mReplenishmentOrderModelList.remove(position);
                adapter.notifyItemRemoved(position);
            }
        });
    }

    /**
     * 提交的弹窗
     */
    private void setDeleteMemberDialog() {
        mSubmitView = View.inflate(activity, R.layout.dialog_delete_member, null);
        TextView dialogTitle = mSubmitView.findViewById(R.id.dialog_title);
        dialogTitle.setText("确认提交补货单");
        mSubmitDialog = DialogUtil.getDialog(activity, true, mSubmitView, ScreenUtil
                .getScreenWidthPix(activity) * 0.9);
        mSubmitDialog.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.replaceFragment(R.id.fl_content,new SubmitReplenishmentFragment(0),true);
                mSubmitDialog.dismiss();
            }
        });
        mSubmitDialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSubmitDialog.dismiss();
            }
        });
    }
}
